package com.example.composewebview.Utils.PermissionUtil

import android.Manifest
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.content.ContextCompat

//相机/拍照/录像权限：
fun checkCameraPermission(context: Context): Boolean {
    val cameraPermission = Manifest.permission.CAMERA
    return ContextCompat.checkSelfPermission(context, cameraPermission) == PackageManager.PERMISSION_GRANTED
}

//联系人读取/写入
fun checkContactsPermission(context: Context): Boolean {
    val readPermission = Manifest.permission.READ_CONTACTS
    val writePermission = Manifest.permission.WRITE_CONTACTS
    val readPermissionResult = ContextCompat.checkSelfPermission(context, readPermission)
    val writePermissionResult = ContextCompat.checkSelfPermission(context, writePermission)
    return readPermissionResult == PackageManager.PERMISSION_GRANTED && writePermissionResult == PackageManager.PERMISSION_GRANTED
}

//仅联系人读取。iOS中等同于contacts。
fun checkContactsReadPermission(context: Context): Boolean {
    val contactsPermission = Manifest.permission.READ_CONTACTS
    return ContextCompat.checkSelfPermission(context, contactsPermission) == PackageManager.PERMISSION_GRANTED
}

//仅联系人写入。iOS中等同于contacts。
fun checkContactsWritePermission(context: Context): Boolean {
    val contactsWritePermission = Manifest.permission.WRITE_CONTACTS
    return ContextCompat.checkSelfPermission(context, contactsWritePermission) == PackageManager.PERMISSION_GRANTED
}

//使用麦克风录制音频权限：
fun checkMicrophonePermission(context: Context): Boolean {
    val microphonePermission = Manifest.permission.RECORD_AUDIO
    return ContextCompat.checkSelfPermission(context, microphonePermission) == PackageManager.PERMISSION_GRANTED
}


//访问相册/本地存储空间权限：
fun checkPhotosPermission(context: Context): Boolean {
    val photosPermission = Manifest.permission.READ_EXTERNAL_STORAGE
    return ContextCompat.checkSelfPermission(context, photosPermission) == PackageManager.PERMISSION_GRANTED
}

fun checkPhotosWritePermission(context: Context): Boolean {
    val photosWritePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
    return ContextCompat.checkSelfPermission(context, photosWritePermission) == PackageManager.PERMISSION_GRANTED
}


//定位权限：
fun checkLocationPermission(context: Context): Boolean {
    val locationPermission = Manifest.permission.ACCESS_FINE_LOCATION
    return ContextCompat.checkSelfPermission(context, locationPermission) == PackageManager.PERMISSION_GRANTED
}


//状态栏通知权限：
fun checkNotificationPermission(context: Context): Boolean {
    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    return notificationManager.areNotificationsEnabled()
}

//仅直接拨打电话权限：
fun checkPhoneCallPermission(context: Context): Boolean {
    val phoneCallPermission = Manifest.permission.CALL_PHONE
    val permissionResult = ContextCompat.checkSelfPermission(context, phoneCallPermission)
    return permissionResult == PackageManager.PERMISSION_GRANTED
}

// Check phone read permission
fun checkPhoneReadPermission(context: Context): Boolean {
    val phoneReadPermission = Manifest.permission.READ_PHONE_STATE
    val permissionResult = ContextCompat.checkSelfPermission(context, phoneReadPermission)
    return permissionResult == PackageManager.PERMISSION_GRANTED
}

// Check phone call log read permission
fun checkPhoneCallLogReadPermission(context: Context): Boolean {
    val phoneCallLogReadPermission = Manifest.permission.READ_CALL_LOG
    val permissionResult = ContextCompat.checkSelfPermission(context, phoneCallLogReadPermission)
    return permissionResult == PackageManager.PERMISSION_GRANTED
}

// Check phone call log write permission
fun checkPhoneCallLogWritePermission(context: Context): Boolean {
    val phoneCallLogWritePermission = Manifest.permission.WRITE_CALL_LOG
    val permissionResult = ContextCompat.checkSelfPermission(context, phoneCallLogWritePermission)
    return permissionResult == PackageManager.PERMISSION_GRANTED
}


//传感器权限：
fun checkSensorPermission(context: Context): Boolean {
    val sensorPermission = Manifest.permission.BODY_SENSORS
    return ContextCompat.checkSelfPermission(context, sensorPermission) == PackageManager.PERMISSION_GRANTED
}

//读取短信/后台发送短信权限：
fun checkSmsPermission(context: Context): Boolean {
    val smsPermission = Manifest.permission.READ_SMS
    return ContextCompat.checkSelfPermission(context, smsPermission) == PackageManager.PERMISSION_GRANTED
}

fun checkSmsSendPermission(context: Context): Boolean {
    val smsSendPermission = Manifest.permission.SEND_SMS
    return ContextCompat.checkSelfPermission(context, smsSendPermission) == PackageManager.PERMISSION_GRANTED
}

//仅后台发送短信权限：
fun checkSmsSendBackgroundPermission(context: Context): Boolean {
    val smsSendBackgroundPermission = Manifest.permission.SEND_SMS
    return (ContextCompat.checkSelfPermission(context, smsSendBackgroundPermission) == PackageManager.PERMISSION_GRANTED
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
}



//读取/写入|相册|多媒体|本地存储空间权限
fun checkStoragePermission(context: Context): Boolean {
    val storagePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
    return ContextCompat.checkSelfPermission(context, storagePermission) == PackageManager.PERMISSION_GRANTED
}


//仅读取|相册|多媒体|文件|本地存储空间权限：
fun checkStorageReadPermission(context: Context): Boolean {
    val storageReadPermission = Manifest.permission.READ_EXTERNAL_STORAGE
    return ContextCompat.checkSelfPermission(context, storageReadPermission) == PackageManager.PERMISSION_GRANTED
}

//仅写入|相册|多媒体|文件|本地存储空间权限：
fun checkStorageWritePermission(context: Context): Boolean {
    val storageWritePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
    return ContextCompat.checkSelfPermission(context, storageWritePermission) == PackageManager.PERMISSION_GRANTED
}

//蓝牙扫描权限（Android 12 及以上）：
fun checkBleScanPermission(context: Context): Boolean {
    val bleScanPermission = Manifest.permission.BLUETOOTH_SCAN
    return (ContextCompat.checkSelfPermission(context, bleScanPermission) == PackageManager.PERMISSION_GRANTED
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
}

//蓝牙广告权限（Android 12 及以上）
fun checkBleAdvertisePermission(context: Context): Boolean {
    val bleAdvertisePermission = Manifest.permission.BLUETOOTH_ADVERTISE
    return (ContextCompat.checkSelfPermission(context, bleAdvertisePermission) == PackageManager.PERMISSION_GRANTED
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
}

//蓝牙连接权限（Android 12 及以上）：
fun checkBleConnectPermission(context: Context): Boolean {
    val bleConnectPermission = Manifest.permission.BLUETOOTH_CONNECT
    return (ContextCompat.checkSelfPermission(context, bleConnectPermission) == PackageManager.PERMISSION_GRANTED
            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
}

